package czk.concrent.synControl;

import java.util.concurrent.locks.LockSupport;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/6/2 21:31
 */
// LockSupport交替输出
public class Test3 {
    static Thread t1;
    static Thread t2;
    static Thread t3;
    public static void main(String[] args) {
        ParkUnpark parkUnpark=new ParkUnpark(5);
        // 线程一
        t1=new Thread(()->{
            parkUnpark.print("a",t2);
        });
        // 线程二
        t2=new Thread(()->{
            parkUnpark.print("b",t3);
        });
        // 线程三
        t3=new Thread(()->{
            parkUnpark.print("c",t1);
        });
        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);
    }
}
class ParkUnpark{
    private int loopNumber;

    public ParkUnpark(int loopNumber) {
        this.loopNumber = loopNumber;
    }
    public void print(String content,Thread next){
        for (int i = 0; i < loopNumber; i++) {
            // 锁住当前线程
            LockSupport.park();
            // 唤醒后解锁下一线程
            System.out.print(content);
            LockSupport.unpark(next);
        }
    }
}
